<?php
/**
 * @return XAnnotation
 */	
function a($class){
	return XAnnotation::singleton($class);
}
/**
 * @return object
 */	
function aop($class, $args = array()){
	return XAnnotationAOPFactory::singleton()->get($class, $args);
}
/**
 * @return object
 */
function i($class, $args = array()){
	return XFactory::singleton()->get($class, $args);
}
function cf($key){
	return XConfig::singleton()->get($key);
}
function g(){
	
}
function p($output){
	if(!is_object($output) && !is_array($output)){
		echo "<xmp>".$output."</xmp>";
	}elseif(is_array($output)){
		echo "<xmp>".var_export($output)."</xmp>";
	}else{
		//TODO
		echo "<xmp>".var_export($output)."</xmp>";
	}
}
function pe(XEvent $event){
	XEventContext::singleton()->publish($event);
}
/**
 * @return XARQuery
 */
function q($model, $key = null, $value = null){
	$query = null;
	$loader = XClassLoader::loader($model);
	if($loader->isSubclassOf('XARShardRecord')){
		$query = XARShardQuery::get($model);
	}elseif($loader->isSubclassOf('XARPartitionRecord')){
		$query = XARPartitionQuery::get($model);
	}else{
		$query = XARQuery::get($model);
	}
	if(!is_null($key) && !is_null($value)){
		return $query->eq($key, $value)
			->find();
	}
	if(!is_null($key)){
		return $query->find($key);		
	}
	return $query;
}
function s(){
	
}
function m($key){
	return XI18N::singleton()->message($key);
}
function i18n($locale = null){
	if(!is_null($locale)){		
		XI18N::singleton()->setLocale($locale);
	}
	return XI18N::singleton()->getLocale();
}
function x_array_get(&$array, $key){
	if(!is_array($array) || !$key){
		return null;
	}
	if(isset($array[$key])){
		return $array[$key];
	}
	preg_match("/^([^\.]+)\.*(([^\.]+\.*)+)$/i", $key, $match);	
	if(!isset($match[1])
		|| !isset($match[2])
		|| !isset($array[$match[1]])
		|| !is_array($array[$match[1]])){
		return null;
	}
	return x_array_get($array[$match[1]], $match[2]);
}
function x_array_head($array){
	if(!is_array($array) || count($array) == 0){
		return null;
	}
	$result = array_slice($array, 0, 1);
	return array_shift($result);
}
function x_array_pick($array, $key){
	if(!is_array($array) || count($array) == 0){
		return array();
	}
	$results = array();
	foreach($array as $value){
		if(is_array($value)
			&& isset($value[$key])){			
			$results[] = $value[$key];
			continue;
		}
		if(is_object($value)){			
			$results[] = $value->$key;
		}
	}
	return $results;
}
function x_array_set(&$array, $key, $value, $split = false){
	if(!is_array($array) || !$key){
		return $array;
	}
	if(!$split || !preg_match("/([^\.]+)\.([^\.]+)/i", $key)){
		if(!isset($array[$key])){			
			$array[$key] = $value;		
			return $array;
		}
		if(!is_array($array[$key])
			&& !is_array($value)){
			$array[$key] = array($array[$key], $value);
		}elseif(!is_array($array[$key])){
			$array[$key] = array_merge(array($array[$key]), $value);					
		}elseif(!is_array($value)){
			$array[$key] = array_merge($array[$key], array($value));	
		}else{
			foreach($value as $newKey => $newValue){				
				$array[$key] = x_array_set($array[$key], $newKey, $newValue, $split);
			}
		}
		return $array;
	}
	preg_match("/^([^\.]+)\.*(([^\.]+\.*)+)$/i", $key, $match);
	if(!isset($array[$match[1]])){
		$array[$match[1]] = array();
	}
	return x_array_set($array[$match[1]], $match[2], $value, true);
}
function x_array_tail($array){
	if(!is_array($array) || count($array) == 0){
		return null;
	}
	$result = array_slice($array, count($array)-1, 1);
	return array_pop($result);
}
function x_class_name($name){
	return x_name_to_java(preg_replace("/\.php$/i", "", $name));
}
function x_name_to_c($name){	
	$str = preg_replace("/^([A-Z]+)([A-Z])/e", "strtolower('\\1').'_'.strtolower('\\2')", $name);
	$str = preg_replace("/^([A-Z])/e", "strtolower('\\1')", $str);
	$str = preg_replace("/([A-Z]+)$/e", "'_'.strtolower('\\1')", $str);
	$str = preg_replace("/([A-Z])/e", "'_'.strtolower('\\1')", $str);
	return strtolower($str);
}
function x_name_to_java($name){
	return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));
}
function x_preg_split_tail($pattern, $subject){
	$array = preg_split($pattern, $subject, 0, PREG_SPLIT_NO_EMPTY);
	return array_pop($array);
}
function x_require($file){
	if(file_exists($file)){
		require_once($file);
	}
}
function x_debug($source, $message){
	pe(new XLoggingEvent($source, DEBUG, $message));
}
function x_info($source, $message){
	pe(new XLoggingEvent($source, INFO, $message));
}
function x_warn($source, $message){
	pe(new XLoggingEvent($source, WARN, $message));
}
function x_error($source, $message, Exception $exception = null){
	pe(new XLoggingEvent($source, ERROR, $message, $exception));
}
?>